/* 
 * javascript start by hieubd
 */
//Tipsy Tooltips
	$('.tooltip').tipsy({fade: true});
	$('.tooltip.north').tipsy({fade: true, gravity: 's'});
	$('.tooltip.east').tipsy({fade: true, gravity: 'w'});
	$('.tooltip.west').tipsy({fade: true, gravity: 'e'});
	// Form Tooltips
	$('form [title]').tipsy({fade: true, trigger: 'focus', gravity: 'w'});

	//Form Switches
$(".switch-enable").click(function(){
	      var parent = $(this).parents('.switch-wrapper');
	      if($('.status',parent).val()==0){
		      var r = confirm("Do you really want to change this?");
		      var data = "id="+$('.change',parent).val()+"&status="+$('.status',parent).val();
		  	  var url = "/admin/index/changestatus";
			  if(r == true){	    
			  	 	$.ajax({ 
			  	    	url : url,
			  	    	type: 'POST',
			  	    	cache : false,
			  	    	data : data,
			  	    	success:function(){
			  	    			$('.switch-disable',parent).removeClass('selected');
				  	  	      	$('.switch-enable',parent).addClass('selected');
					  	  	    $('.status',parent).val(1);
			  	    	},
			  	    	error : function (data){ 
			  	    		alert(data);
			  	    	}
			  	    });
			}
	    }
	});
$(".switch-disable").click(function(){
    var parent = $(this).parents('.switch-wrapper');
    if($('.status',parent).val()==1){
	    var r = confirm("Do you really want to change this?");
	    var data = "id="+$('.change',parent).val()+"&status="+$('.status',parent).val();
		var url = "/admin/index/changestatus";
		if(r == true){	    
	  	 	$.ajax({ 
	  	    	url : url,
	  	    	type: 'POST',
	  	    	cache : false,
	  	    	data : data,
	  	    	success:function(){
	  	    			$('.switch-enable',parent).removeClass('selected');
					    $('.switch-disable',parent).addClass('selected');
					    $('.status',parent).val(0);
	  	    	},
	  	    	error : function (data){ 
	  	    		alert(data);
	  	    	}
	  	    });
	}
    }
});
// Delete product 
$(".delete-product").click(function(){
    var parent = $(this).parents('.options');
    var tr_parent = $(this).parents('tr');
	var r = confirm("Do you really want to delete this?");
	var data = "id="+$('.id-product',parent).val();
	var url = "/admin/index/deleteproduct";
	if(r == true){	    
	  	 $.ajax({ 
	  	    	url : url,
	  	    	type: 'POST',
	  	    	cache : false,
	  	    	data : data,
	  	    	success:function(result){
//		  	    	if(result){
//	  	    			$('tr',tr_parent).remove();
//		  	    	}
	  	    	},
	  	    	error : function (data){ 
	  	    		//alert(data);
	  	    	}
	  	    });
	}
});
/*
 * javascipt end by hieubd
 */
$(document).ready(function() {
	init();
	  $('#icon_upload').uploadify({
		    'uploader'  : '/js/uploadify.swf',
		    'script'    : '/js/uploadify.php',
		    'cancelImg' : '/images/cancel.png',
		    'folder'    : '/images/icon',
		 	'onComplete'  : function(event, ID, fileObj, response, data) {   
			  	showimage("icon",response);
		  		},
		    'auto'      : true
	  });
  $('#intro_upload').uploadify({
	    'uploader'  : '/js/uploadify.swf',
	    'script'    : '/js/uploadify.php',
	    'cancelImg' : '/images/cancel.png',
	    'folder'    : '/images/intro-images',
	 	'onComplete'  : function(event, ID, fileObj, response, data) {   
		  	showimage("intro_image",response);
	  		},
	    'auto'      : true
  });
  $('#poster_upload').uploadify({
	    'uploader'  : '/js/uploadify.swf',
	    'script'    : '/js/uploadify.php',
	    'cancelImg' : '/images/cancel.png',
	    'folder'    : '/images/poster',
	 	'onComplete'  : function(event, ID, fileObj, response, data) {   
		  	showimage("poster",response);
	  		},
	    'auto'      : true
  });
  $('#file_upload1').uploadify({
	    'uploader'  : '/js/uploadify.swf',
	    'script'    : '/js/uploadify.php',
	    'cancelImg' : '/images/cancel.png',
	    'folder'    : '/file',
	 	'onComplete'  : function(event, ID, fileObj, response, data) {   
		  	showfilename("link_download1",response);
	  		},
	    'auto'      : true
  });
  
 
  
  $('#banner_upload').uploadify({
	    'uploader'  : '/js/uploadify.swf',
	    'script'    : '/js/uploadify.php',
	    'cancelImg' : '/images/cancel.png',
	    'folder'    : '/images/icon',
	 	'onComplete'  : function(event, ID, fileObj, response, data) {   
	  				showbanner(response);
	  		},
	    'auto'      : true
});
});

function showimage(name_id,image_link){
	jQuery("#"+name_id).val(image_link);
	var image = "<img src='"+image_link+"' />";
	jQuery("#show_"+name_id).html(image);
}

function showfilename(name_id,file_link){
//	 alert(file_link);
//	 alert(name_id);
	jQuery("#"+name_id).val(file_link);
//	jQuery("#show_"+name_id).html(file_link);
}

function search(){
	var key=jQuery("#search").val().trim();
	var type=jQuery("#type").val();
	if (key){
		jQuery.ajax({
			type:"POST",
			url:"/admin/index/search",
			data:"key="+key+"&type="+type,
			success:function(html){
				jQuery("#list_product").html(html);
		}
		})
	}
}

function edit(id){
	window.location = "/admin/index/editproduct?id="+id;
}

function del(id){
	window.location = "/admin/index/delete?id="+id;
}

function showbanner(banner_link){
	var image = "<img src='"+banner_link+"' height=200px />"
	jQuery("#show_banner").append(image);
	jQuery("#banner").val(banner_link);
}

function delbanner(id){
	jQuery("#banner_"+id).hide();
	jQuery.ajax({
		type:"POST",
		url:"/admin/index/delbanner",
		data:"id="+id	
	});
}

function addsite(){
	var site = jQuery("#add_site").val();
	jQuery.ajax({
		type:"POST",
		url:"/admin/index/addsite",
		data:"site="+site,
		success:function(html){
			jQuery("#site_table").append(html);
	}
	})
}

function showFormAdd(){
	$("#formadd").show();
}
function showFormEdit(id){
//	alert(id);
	$("#formadd").hide();
	$("#formedit").show();
	$("#temp").val(id);
}

function edit_acount(id){
	
	url = '/admin/admin/editacount?id='+id;
	$("#formnew").load(url);
	
	$("#formnew").fadeIn();
	$("#formadd").remove();
}

function editsite(id){
	
	url = '/admin/index/editsite?id='+id;
	$("#formnew").load(url);
	
	$("#formnew").fadeIn();
	$("#formadd").remove();
}

function editcampaign(id){
	
	url = '/admin/admin/editcampaign?id='+id;
	$("#formnew").load(url);
	
	$("#formnew").fadeIn();
	$("#formadd").remove();
}

function delete_acount(id){
//	alert(id);
	var comfirmBox;
	 comfirmBox = confirm("Bạn có chác chắn muốn xóa không ?");
	 if(comfirmBox == true)
	  {
		 $.ajax(
			      {
			    	  type:"POST",
					  url: "/admin/admin/deleteacount",
					  data: "id=" + id,
					  datatype: "html",
					  success: function(html)
					     {
						  $("#" + id).remove();
					     }
			      }
			      );
	  }
}

function delete_site(id){
	var comfirmBox;
	 comfirmBox = confirm("Bạn có chác chắn muốn xóa không ?");
	 if(comfirmBox == true)
	  {
		 $.ajax(
			      {
			    	  type:"POST",
					  url: "/admin/index/deletesite",
					  data: "id=" + id,
					  datatype: "html",
					  success: function(html)
					     {
						  $("#" + id).remove();
					     }
			      }
			      );
	  }
}

function delete_campaign(id){
	var comfirmBox;
	// alert(id);
	 comfirmBox = confirm("Bạn có chác chắn muốn xóa không ?");
	 if(comfirmBox == true)
	  {
		 $.ajax(
			      {
			    	 
			    	  type:"POST",
					  url: "/admin/admin/deletecampaign",
					  data: "id=" + id,
					  datatype: "html",
					  success: function(html)
					     {
						  $("#" + id).remove();
					     }
			      }
			      );
	  }
}

function edit_status(id,status){
	   var comfirmBox;
	   comfirmBox = confirm("Bạn có chắc chắn muốn thay đổi ?");
	  // alert(status);
	   if(comfirmBox == true)
		  {
		   $.ajax(
		      {
		    	  type:"POST",
				  url: "/admin/admin/editstatus",
				  data: "id=" + id + "&status=" + status,
				  datatype: "html",
				  success: function(html)
				     {
					  
						    $("#" + id).empty();
						    $("#"+ id).prepend(html);
					    
					   // else  $("#" + id).remove();
					 }
			 });
		   
		  }
}

function loadupload(){
	var number_of_option = Number($("#count_upload").val());
	$("#count_upload").val(number_of_option + 1);
	$.ajax(
		      {
		    	  type:"POST",
				  url: "/admin/index/uploadnew",
				  data: "count="+ (number_of_option + 1),
				  datatype: "html",
				  success: function(html)
				     {
						    $("#upload").append(html);
					 }
			 });
}

function addupload(){
	var number_of_option = Number($("#number_upload_new").val());
	$("#number_upload_new").val(number_of_option + 1);
//	$("#index_max").val(number_of_option + 1);
	$.ajax(
		      {
		    	  type:"POST",
				  url: "/admin/index/addupload",
				  data: "count="+ (number_of_option + 1),
				  datatype: "html",
				  success: function(html)
				     {
						    $("#addupload").append(html);
					 }
			 });
}

function sub_upload_editproduct(id){
	$("#uploadnew_editproduct"+id).remove();
//	var number_of_option = Number($("#number_upload_new").val());
//	$("#number_upload_new").val(number_of_option - 1);
}

function sub_upload(id){
	$("#uploadnew"+id).remove();
//	var number_of_option = Number($("#count_upload").val());
//	$("#count_upload").val(number_of_option - 1);
}

function init(){
	 $('.file_upload_new').uploadify({
		    'uploader'  : '/js/uploadify.swf',
		    'script'    : '/js/uploadify.php',
		    'cancelImg' : '/images/cancel.png',
		    'folder'    : '/file',
		 	'onComplete'  : function(event, ID, fileObj, response, data) {   
			  	
		  		},
		    'auto'      : true
	});
}

function deletefile(link_file,id){
//	alert(id);
	var comfirmBox;
	 comfirmBox = confirm("Bạn có chác chắn muốn xóa không ?");
	 if(comfirmBox == true)
	  {
			$.ajax(
				      {
				    	  type:"POST",
						  url: "/admin/index/deletefile",
						  data: "link=" + link_file + "&id=" + id,
						  datatype: "html",
						  success: function(html)
						     {
							//  alert(html);
							   $("#" + id).remove();
						     }
				      }
				      );
	  }
}